package com.xucy.springboot.controller;

import com.xucy.springboot.entity.CommonResult;
import com.xucy.springboot.entity.Student;
import com.xucy.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studentCon")
//开启PreAuthorize注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class StudentController {

    @Autowired
    public StudentService studentService;

    @GetMapping("/testRoleAuth")
    //方法执行前进行角色验证 @PostAuthorize("returnObject%2==0") 执行后认证
    // @PreFilter(filter)  对集合传入参数进行过滤 PostFilter 对集合返回参数进行过滤
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PreFilter("filterObject%2==0")
    public String testRoleAuth(){
        return "测试角色验证";
    }

    @GetMapping("/testRoleAuth1")
    //方法执行前 判断参数id是否小于10 ，userName是否和userDetails中的username一致
    @PreAuthorize("#id <10 and principal.username.equals(#userName)")
    public String testRoleAuth1(String id ,String userName){
        return "测试角色验证";
    }

    //添加一个学生
    @PostMapping(value = "/addStudent")
    public CommonResult addStudent(Student student) {
        CommonResult result = new CommonResult();
        try {
            studentService.saveStudent(student);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //修改一个学生(jpa是根据id来修改的)
    @PutMapping(value = "/updateStudent")
    public CommonResult updateStudentById(Student student) {
        CommonResult result = new CommonResult();
        try {
            studentService.updateStudent(student);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //根据id删除一条数据
    @DeleteMapping(value = "/deleteStudent/{id}")
    public CommonResult deleteStudentById(@PathVariable(name = "id", required = true) Long id) {
        CommonResult result = new CommonResult();
        try {
            studentService.deleteStudent(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //查询所有
    @GetMapping(value = "/findAll")
    public CommonResult findAll() {
        CommonResult result = new CommonResult();
        try {
            List<Student> list = studentService.findAll();
            //将查询结果封装到CommonResult中
            result.setData(list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //根据id查询一条数据(2.0后不能使用findOne了)
    @GetMapping(value = "/findStudentById/{id}")
    public CommonResult findStudentById(@PathVariable(name = "id") Long id) {
        CommonResult result = new CommonResult();
        try {
            Student student = studentService.fingById(id);
            //将查询结果封装到CommonResult中
            result.setData(student);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //根据学生姓名查询多条数据
    @GetMapping(value = "/findStudentByName")
    public CommonResult findStudentByName(String name) {
        CommonResult result = new CommonResult();
        try {
            List<Student> studentList = studentService.findByName(name);
            //将查询结果封装到CommonResult中
            result.setData(studentList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //根据学生姓名查询多条数据
    @GetMapping(value = "/findByLastName")
    public CommonResult findByLastName(String name) {
        CommonResult result = new CommonResult();
        try {
            List<Student> studentList = studentService.findByLastName(name);
            //将查询结果封装到CommonResult中
            result.setData(studentList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }


    //查询所有分页
    @GetMapping(value = "/fingAllPage")
    public CommonResult fingAllPage() {
        CommonResult result = new CommonResult();
        try {
            List<Student> list = studentService.fingAllPage();
            //将查询结果封装到CommonResult中
            result.setData(list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }

    //查询所有排序
    @GetMapping(value = "/findAllSort")
    public CommonResult findAllSort() {
        CommonResult result = new CommonResult();
        try {
            List<Student> list = studentService.findAllSort();
            //将查询结果封装到CommonResult中
            result.setData(list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(500);
            result.setMsg("失败");
            return result;
        }
    }
}
